package com.macy.didi;


import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.ViewCompat;

import static com.macy.didi.DidiLayout.State.EXPANDED;


/**
 * @author macy
 */
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private DidiLayout   mLayout;
    private LinearLayout mLlNav;
    private LinearLayout hd;
    private LinearLayout mTvBack;
    private LinearLayout mMapView;
    private ScrollLayout mScrollLayout;
    private WebView      mywebview;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);





        mLayout = findViewById(R.id.sliding_layout);
        mMapView = findViewById(R.id.map);
        mScrollLayout = findViewById(R.id.scroll);
        mLlNav = findViewById(R.id.ll_nav);
        mywebview = findViewById(R.id.mywebview);
        hd = findViewById(R.id.hd);

        mywebview.loadUrl("https://www.baidu.com");

        //hd.setBackgroundColor(0x99000000);

        mMapView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mScrollLayout.setCanScroll(false);
                        break;
                    case MotionEvent.ACTION_UP:
                        mScrollLayout.setCanScroll(true);
                        break;
                    default:
                        break;
                }
                return false;
            }
        });

        mLayout.addPanelSlideListener(new DidiLayout.PanelSlideListener() {
            @Override
            public void onPanelSlide(View panel, float slideOffset) {
                int x=(int)(255*slideOffset);

                //Log.d("xxx","slideOffset:"+slideOffset+"  255百分比:"+Math.abs(255-x));

                mMapView.getBackground().mutate().setAlpha(Math.abs(255-x));
            }

            @Override
            public void onStateChanged(View panel, DidiLayout.State previousState, DidiLayout.State newState) {
                Log.d("xxx","newState:"+newState);

                if (newState == EXPANDED) {
                    mTvBack.setVisibility(View.VISIBLE);
                    ViewCompat.setTranslationY(mLlNav, -mLlNav.getHeight());
                    mScrollLayout.setCanScroll(true);
                } else {
                    mTvBack.setVisibility(View.GONE);
                    ViewCompat.setTranslationY(mLlNav, 0);
                    mScrollLayout.setCanScroll(false);
                }
            }

            @Override
            public void onMainSlide(float mainViewOffset) {
                Log.d("xxx","mainViewOffset:"+mainViewOffset);
                if (mainViewOffset==0){
                    mScrollLayout.setCanScroll(false);
                }
            }
        });
        mLayout.setFadeOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mLayout.setState(DidiLayout.State.COLLAPSED);
            }
        });


        mTvBack = findViewById(R.id.tv_back);
        mTvBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mLayout.setState(DidiLayout.State.COLLAPSED);
                mScrollLayout.smoothScrollTo(0, 0);
            }
        });


        //makeStatusBarTransparent(this);
    }


    @Override
    public void onBackPressed() {
        if (mLayout != null && (mLayout.getState() == EXPANDED || mLayout.getState() == DidiLayout.State.ANCHORED)) {
            mLayout.setState(DidiLayout.State.COLLAPSED);
            mScrollLayout.smoothScrollTo(0, 0);
        } else {
            super.onBackPressed();
        }
    }


    public  void makeStatusBarTransparent(Activity activity) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return;
        }
        Window window = activity.getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            int option = window.getDecorView().getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
            window.getDecorView().setSystemUiVisibility(option);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }


}
